/***************************************************************************
 *   This file is part of KLabeler                                         *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "./ontologymanager.h"

#include <QtCore/QSharedDataPointer>
#include <QtCore/QList>

#include <nepomuk/resource.h>
#include <nepomuk/nepomuk_export.h>

#include <Soprano/Model>
#include <Soprano/QueryResultIterator>

#include <nepomuk/resourcemanager.h>
#include <nepomuk/resource.h>
#include <nepomuk/thing.h>
#include <nepomuk/variant.h>

#include <Soprano/Node>
#include <Soprano/Vocabulary/XMLSchema>

#include <Nepomuk/ResourceManager>
#include <Nepomuk/Resource>
#include <Nepomuk/Types/Property>
#include <Nepomuk/Types/Class>
#include <Nepomuk/Types/Literal>

#include <Nepomuk/Tag>

#include <Soprano/Version>
#include <Soprano/Model>
#include <Soprano/QueryResultIterator>
#include <Soprano/Node>
#include <Soprano/Statement>
#include <Soprano/LiteralValue>
#include <Soprano/StatementIterator>
#include <Soprano/Vocabulary/RDF>
#include <Soprano/Vocabulary/RDFS>
#include <Soprano/Vocabulary/NRL>
#include <Soprano/Vocabulary/NAO>
#include <Soprano/Vocabulary/XMLSchema>
#include <Soprano/Vocabulary/OWL>
#include <Soprano/Vocabulary/Xesam>


OntologyManager::OntologyManager()
{
    Nepomuk::ResourceManager::instance()->init();
}

OntologyManager::~OntologyManager()
{

}



void OntologyManager::start()
{
}

void OntologyManager::lookAllResources()
{
    QString query = QString(
                        " select distinct ?r ?t ?p where { "
                        " ?r ?t ?p "
                        " } ")
                    ;
    printf("QUERY: %s\n\n", query.toAscii().data());
    Soprano::QueryResultIterator it = Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(query,
                                      Soprano::Query::QueryLanguageSparql);
    while (it.next()) {
        printf("%s\t|\t%s\t|\t%s\n", it.binding("t").toString().toAscii().data(),
               it.binding("r").toString().toAscii().data(),
               it.binding("p").toString().toAscii().data());
    }
    printf("--------------\n");
}

void OntologyManager::lookForTags()
{
    QString query = QString(
                        "PREFIX nao: <%1> "
                        " select distinct ?r ?t ?l where { "
                        " ?r %2 ?t ."
                        " ?t %3 ?l ."
                        " } ")
                    .arg(Soprano::Vocabulary::NAO::naoNamespace().toString())   //1
                    .arg(Soprano::Node::resourceToN3(Soprano::Vocabulary::NAO::hasTag()))   //2
                    .arg(Soprano::Node::resourceToN3(Soprano::Vocabulary::NAO::prefLabel()))   //3
                    //.arg( Soprano::Node::resourceToN3(Soprano::Vocabulary::RDFS::label()) ) //3
                    ;
    printf("QUERY: %s\n\n", query.toAscii().data());
    Soprano::QueryResultIterator it = Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(query,
                                      Soprano::Query::QueryLanguageSparql);
    printf("TAG: ----------------------\n");
    while (it.next()) {
        printf("%s\t%s\t%sn", it.binding("r").toString().toAscii().data(), it.binding("l").toString().toAscii().data(), it.binding("t").toString().toAscii().data());
    }
    printf("--------------\n");
}
void OntologyManager::lookForObjectsWithATag()
{

    QString query = QString(
                        "PREFIX nao: <%1> "
                        " select distinct ?r where { "
                        //" GRAPH ?g { ?r rdf:type nao:Tag } . "
                        " } ")
                    .arg(Soprano::Vocabulary::NAO::naoNamespace().toString())   //1
                    ;
    printf("QUERY: %s\n\n", query.toAscii().data());
    Soprano::QueryResultIterator it = Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(query,
                                      Soprano::Query::QueryLanguageSparql);
    while (it.next()) {
        printf("%s\t\n", it.binding("r").toString().toAscii().data());
    }
}


void OntologyManager::showResourcesOfTags(const QString &tagLabel)
{
    QString query = QString(
                        "PREFIX nao: <%1> "
                        " select distinct ?r where { "
                        " ?r <%2> \"%3\" "
                        " } ")
                    .arg(Soprano::Vocabulary::NAO::naoNamespace().toString())   //1
                    .arg(Soprano::Vocabulary::NAO::Tag().toString())   //2
                    .arg(tagLabel)   //3
                    ;
    printf(" Show All resources of tag QUERY: %s\n\n", query.toAscii().data());
    Soprano::QueryResultIterator it = Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(query,
                                      Soprano::Query::QueryLanguageSparql);
    printf("TAG: ----------------------\n");
    while (it.next()) {
        printf("%s\t\n", it.binding("r").toString().toAscii().data());
    }
}

void OntologyManager::showAllTags()
{
    QString query = QString(
                        " select distinct ?r ?t ?p where { "
                        " ?r ?t ?p "
                        " } ")
                    ;
    printf("show all tags QUERY: %s\n\n", query.toAscii().data());
    Soprano::QueryResultIterator it = Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(query,
                                      Soprano::Query::QueryLanguageSparql);
    while (it.next()) {
        printf("%s\t|\t%s\t|\t%s\n", it.binding("t").toString().toAscii().data(),
               it.binding("r").toString().toAscii().data(),
               it.binding("p").toString().toAscii().data());
    }
    printf("--------------\n");
}

void OntologyManager::showResourceTags(const QString &resource)
{
    QString query = QString(
                        "PREFIX nao: <%1> "
                        " select distinct ?r ?t ?l where { "
                        " ?r %2 ?t ."
                        " ?t %3 ?l ."
                        " ?r %4 <%5> ."
                        " } ")
                    .arg(Soprano::Vocabulary::NAO::naoNamespace().toString())   //1
                    .arg(Soprano::Node::resourceToN3(Soprano::Vocabulary::NAO::hasTag()))   //2
                    .arg(Soprano::Node::resourceToN3(Soprano::Vocabulary::NAO::prefLabel()))   //3
                    .arg("<http://www.semanticdesktop.org/ontologies/2007/01/19/nie#url>")  //4
                    //.arg( Soprano::Node::resourceToN3(Soprano::Vocabulary::NIE::url() ) ) //4
                    //.arg(Soprano::Node(resource).toN3() ) //5
                    .arg(resource) //5
                    //.arg( Soprano::Node::resourceToN3(Soprano::Vocabulary::XMLSchema::xxlsNamespace()) ) //6
                    ;
    printf("QUERY: %s\n\n", query.toAscii().data());
    Soprano::QueryResultIterator it = Nepomuk::ResourceManager::instance()->mainModel()->executeQuery(query,
                                      Soprano::Query::QueryLanguageSparql);
    printf("TAG: ----------------------\n");
    while (it.next()) {
        printf("%s\t%s\t%s\n", it.binding("r").toString().toAscii().data(), it.binding("l").toString().toAscii().data(), it.binding("t").toString().toAscii().data());
    }
    printf("--------------\n");
    // second method
    Nepomuk::Resource file(resource, Soprano::Vocabulary::Xesam::File());
    QList<Nepomuk::Tag> tags = file.tags();
    foreach(Nepomuk::Tag tag, tags) {
        printf("TAG: %s\t%s\n", tag.genericLabel().toAscii().data(), tag.label().toAscii().data());
    }
    printf("--------------\n");
}


#include "ontologymanager.moc"
